import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
import '@fortawesome/fontawesome-free/css/all.min.css'
import axios from 'axios'

// 导入样式
import 'ant-design-vue/dist/reset.css'
import '@fortawesome/fontawesome-free/css/all.min.css'

// 配置 axios 默认值
// axios.defaults.baseURL = 'http://localhost:8080'
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Accept'] = 'application/json'

console.log('Axios 配置:', {
  baseURL: axios.defaults.baseURL,
  headers: axios.defaults.headers.common
})

// 请求拦截器
axios.interceptors.request.use(
  config => {
    console.log('请求配置:', config)
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    console.error('请求拦截器错误:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    console.log('响应数据:', response)
    return response
  },
  error => {
    console.error('响应拦截器错误:', error)
    if (error.response && error.response.status === 401) {
      localStorage.removeItem('token')
      localStorage.removeItem('userId')
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

const app = createApp(App)
const pinia = createPinia()

app.use(pinia)
app.use(router)
app.use(Antd)

app.mount('#app')
